package com.example.administrator.wallet.view.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.example.administrator.wallet.R;
import com.example.administrator.wallet.adapter.BackupsGridAdapter;
import com.example.administrator.wallet.base.BaseActivity;
import com.example.administrator.wallet.utils.DimbgUtils;
import com.example.administrator.wallet.utils.ListDataSave;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.example.mylibrary.src.main.java.com.quincysx.crypto.bip39.MnemonicGenerator;
import com.example.mylibrary.src.main.java.com.quincysx.crypto.bip39.RandomSeed;
import com.example.mylibrary.src.main.java.com.quincysx.crypto.bip39.WordCount;
import com.example.mylibrary.src.main.java.com.quincysx.crypto.bip39.wordlists.English;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class BackupsActivity extends BaseActivity {


    @Bind(R.id.finish_img)
    ImageButton finishImg;
    @Bind(R.id.view_title)
    TextView viewTitle;
    @Bind(R.id.linear)
    LinearLayout linear;
    @Bind(R.id.recovery)
    TextView recovery;
    @Bind(R.id.iGridView)
    GridView iGridView;
    @Bind(R.id.submit)
    Button submit;
    private View view;
    private List<String> datas;
    private SharedPreferences wallet;
    private SharedPreferences.Editor edit;
    private List<String> mnemonic;

    @Override
    public void init() {
        wallet = getSharedPreferences("wallet", MODE_PRIVATE);
        edit = wallet.edit();
        setStatuColor();
        initPw();
        initView();
        initData();
        initAdapter();
    }

    private void initData() {
        byte[] random = RandomSeed.random(WordCount.TWELVE);
        mnemonic = new MnemonicGenerator(English.INSTANCE).createMnemonic(random);
        ListDataSave wallet = new ListDataSave(this, "wallet");
        wallet.setDataList("zhujici",mnemonic);
        edit.commit();
    }

    private void initAdapter() {
        BackupsGridAdapter adapter = new BackupsGridAdapter(this, mnemonic);
        iGridView.setAdapter(adapter);
    }

    private void initView() {
        finishImg.setVisibility(View.VISIBLE);
        viewTitle.setText(R.string.copy_zhujici);

    }

    private void initPw() {
        view = View.inflate(this, R.layout.popup_backups, null);
        final Button submit = (Button) view.findViewById(R.id.submit);
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                final PopupWindow window = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
                window.setFocusable(true);
                window.setTouchable(true);
                window.setOutsideTouchable(true);
                findViewById(R.id.linear).post(new Runnable() {
                    @Override
                    public void run() {
                        window.showAtLocation(linear, Gravity.CENTER, 0, 0);
                    }
                });
                Log.e("TAG-----", "OK");
                DimbgUtils.dimBackground(BackupsActivity.this,0.5f);
                window.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        DimbgUtils.dimBackground(BackupsActivity.this, 1.0f);
                    }
                });
                submit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        window.dismiss();
                    }
                });
            }
        });


    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_backups;
    }


    @OnClick({R.id.finish_img, R.id.recovery, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.finish_img:
                finish();
                break;
            case R.id.recovery:
                ActivityUtils.startActivity(this,RecoveryActivity.class);
                break;
            case R.id.submit:
                Intent intent = getIntent();
                String log = intent.getStringExtra("log");
                SharedPrefsUtil.putValue(BackupsActivity.this,"wallet","log",log);
                ActivityUtils.startActivity(this, AgainBackupsActivity.class);
//                String string = getSharedPreferences("wallet", MODE_PRIVATE).getString("logg", "123");
//                finish();
                break;
        }
    }
}
